package com.hongsheng.controller;

/*
@Author：郭振文
@Package：com.hongsheng.controller
@File：UserController
@Date：2023/8/10 22:40
*/

import com.github.pagehelper.PageInfo;
import com.hongsheng.pojo.*;
import com.hongsheng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {


    @Autowired
   private UserService userService;

    //添加用户
    @PostMapping("/insert")
    public R insertUser(@RequestBody User user) {
        if (user.getUsername() == null || user.getUsername().isEmpty()) {
            return new R(false);
        } else if (user.getPassword() == null || user.getPassword().isEmpty()) {
            return new R(false);
        } else if (user.getName() == null || user.getName().isEmpty()) {
            return new R(false);
        } else if (user.getSex() == null) {
            return new R(false);
        } else if (user.getPhone() == null || user.getPhone().isEmpty()) {
            return new R(false);
        } else if (user.getEmail() == null || user.getEmail().isEmpty()) {
            return new R(false);
        } else if (user.getAddress() == null || user.getAddress().isEmpty()) {
            return new R(false);
        } else if (user.getStatus() == null) {
            return new R(false);
        } else {
            Boolean flag = userService.insertUser(user);
            return new R(flag);
        }
    }

    //删除用户
    @DeleteMapping("/{id}")
    public R deleteUser(@PathVariable Integer id){
        Boolean flag = userService.deleteUser(id);
        return new R(flag);
    }

    //通过id查询用户
    @GetMapping("/{id}")
    public R selectUserById(@PathVariable Integer id){
        User user = userService.selectUserById(id);
        return new R(true,user);
    }

    //修改功能查询用户名是否存在
    @PostMapping("/selectUsername")
    public R selectUsername(@RequestBody User user){
        Boolean flag = userService.selectUsername(user);
        System.out.println(flag);
        return new R(flag);
    }

    //添加功能查询用户名是否存在
    @PostMapping("/selectUsername1")
    public R selectUsername1(@RequestBody User user){
        Boolean flag = userService.selectUsername1(user);
        return new R(flag);
    }

    //修改用户
    @PutMapping
    public R updateUser(@RequestBody User user) {
        if (user.getUsername() == null || user.getUsername().isEmpty()) {
            return new R(false);
        } else if (user.getPassword() == null || user.getPassword().isEmpty()) {
            return new R(false);
        } else if (user.getName() == null || user.getName().isEmpty()) {
            return new R(false);
        } else if (user.getSex() == null) {
            return new R(false);
        } else if (user.getPhone() == null || user.getPhone().isEmpty()) {
            return new R(false);
        } else if (user.getEmail() == null || user.getEmail().isEmpty()) {
            return new R(false);
        } else if (user.getAddress() == null || user.getAddress().isEmpty()) {
            return new R(false);
        } else if (user.getStatus() == null) {
            return new R(false);
        } else {
            Boolean flag = userService.updateUser(user);
            return new R(flag);
        }
    }

    //分页条件查询角色
    @GetMapping("{currentPage}/{pageSize}")
    public R selectUser(@PathVariable Integer currentPage,@PathVariable Integer pageSize,User user){
        PageInfo<User> userList = userService.selectUser(currentPage,pageSize,user);
        return new R(true,userList);
    }

    //添加用户角色
    @PostMapping
    public R insertUserRole(@RequestBody User user){
        if (user.getRid()== null || user.getRid().isEmpty()){
            return new R(false);
        }
        List<Integer> ridList = user.getRid();
        Boolean flag = true;
        for (int i = 0; i < ridList.size(); i++) {
            Integer uid = user.getId();
            Integer rid = ridList.get(i);
            UR ur = new UR();
            ur.setUid(uid);
            ur.setRid(rid);
            Boolean flag1 = userService.selectUR(ur);
            if (flag1) {
            flag = userService.insertUserRole(ur);
            if (flag==false){
                break;
            }
        }else {
                return new R(false,"添加角色不能重复");
            }
        }
        return new R(flag);
    }

    //删除用户角色
    @PostMapping("/delete")
    public R deleteUserRole(@RequestBody User user){
        if (user.getRid()== null || user.getRid().isEmpty()){
            return new R(false);
        }
        List<Integer> ridList = user.getRid();
        Boolean flag = true;
        for (int i = 0; i < ridList.size(); i++) {
            Integer uid = user.getId();
            Integer rid = ridList.get(i);
            UR ur = new UR();
            ur.setUid(uid);
            ur.setRid(rid);
            Boolean flag1 = userService.selectUR(ur);
            if (!flag1) {
                flag = userService.deleteUserRole(ur);
                if (flag == false) {
                    break;
                }
            } else {
                return new R(false, "删除角色不存在");
            }
        }
        return new R(flag);
    }



}
